var page = sm("do_Page");
var audio = sm("do_Audio");//音频播放与录制
var imHelper = require("helper/im");
var rootView = ui("$");
var chatName = ui("chatName");
var imgAudioLayout = ui("imgAudioLayout");
var frameL = ui("frameL");
var voiceLen = ui("voiceLen");
var isPlaying = false;//是否正在播放
rootView.setMapping({
	"voiceLen.tag" : "voiceLen",
	"voiceLen.text" : "text",
	"chatName.text":"name",
	"imgHerLogo.source" : "logo",
	"imgAudioLayout.tag" : "voice",
	"imgHerLogo.tag" : "userId",
	"imgAudioTip.tag" : "messageId",
	"imgAudioTip.visible" : "showNewAudio" //是否显示示读语音
});
frameL.startGif("source://image/im/audioL.gif",-1);
rootView.on("dataRefreshed", function(){
	if(chatName.text) chatName.visible = true;
	var voiceNum = voiceLen.tag;
	imgAudioLayout.width = 65+parseInt(voiceNum*1.5);
	imgAudioLayout.redraw();
});

imgAudioLayout.on("touch", function(data, e) {
	var messageId = ui("imgAudioTip").tag;
	if(messageId !=null){
		imHelper.removeReadedAudioId(messageId);//去掉未读语音缓存
		ui("imgAudioTip").hide();
		page.fire("im_chat_page_remove_redtip", messageId);//去掉“未读语音”小红点
	}
	
	if(isPlaying){//如果正在播放，再点击就停止
		stopAudio();
	}else{
		playAudio();
	}
})

//当播放完毕
audio.on("playFinished", function(data) {
	isPlaying = false;
	frameL.startGif("source://image/im/audioL.gif",-1);
});

function playAudio(){
	var voiceFile = imgAudioLayout.tag;
	//deviceone.print("语音remotePath:"+voiceFile);
	if(voiceFile!=null){
		imHelper.loadAudioFile(voiceFile,function(localPath){
			isPlaying = true;
			frameL.startGif("source://image/im/audioLing.gif",-1);
			audio.play(localPath);
		})	
	}
}

function stopAudio(){
	isPlaying = false;
	audio.stop();
	frameL.startGif("source://image/im/audioL.gif",-1);
}

var openHelper = require("helper/open");
ui("do_ALayout_logo").on("touch", function(data){
	if(chatName.text && chatName.text != ""){
		var userId = ui("imgHerLogo").tag;
		if(userId!=null){
			openHelper.openPersonal(userId);//到他的主页
		}
	}
});


